package com.loyal.loyalerp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loyal.loyalerp.basedata.permission.mapper.GyPermissionMapper;
import com.loyal.loyalerp.basedata.permission.model.GyPermission;
import com.loyal.loyalerp.humenresource.role.model.GyRole;
import com.loyal.loyalerp.mapper.GyRolePermissionMapper;
import com.loyal.loyalerp.mapper.GyUserMapper;
import com.loyal.loyalerp.mapper.GyUserRoleMapper;
import com.loyal.loyalerp.model.GyRolePermission;
import com.loyal.loyalerp.model.GyUser;
import com.loyal.loyalerp.model.GyUserRole;
import com.loyal.loyalerp.model.vo.ListRoleVO;
import com.loyal.loyalerp.service.RolePermissionService;

@Service("rolePermissionService")
public class RolePermissionImpl implements RolePermissionService {

	@Autowired
	private GyRolePermissionMapper gyRolePermissionMapper;
	
	// 
	@Autowired
	private GyPermissionMapper gyPermissionMapper;
	
	@Autowired
	private GyUserMapper usermapper;
	
	@Autowired
	private GyUserRoleMapper userRoleMapper;

	//【1】查询显示所有的用户role
	public List<Object> queryByRoles() throws Exception {
		List<Object> obj_List=new ArrayList<Object>();
	    List<GyRolePermission> gyRolePermission_list=gyRolePermissionMapper.queryByRoles();
	  //【1.1】rolePermission部分：查询所有权限，返回权限名
	   List<GyPermission> gyPermission_list=gyPermissionMapper.queryAllPermisssion();
	   obj_List.add(gyRolePermission_list);
	   obj_List.add(gyPermission_list);
	   return obj_List;
	}

	//【2】
	public List<GyRolePermission> getRoleIdToQuery(Integer roleid) throws Exception {
		return gyRolePermissionMapper.getRoleIdToQuery(roleid);
	}
	//
		public List<GyRolePermission> getpermissionIdToQuery(Integer permissionid) throws Exception {
			//List<GyRolePermission> list=gyRolePermissionMapper.getpermissionIdToQuery(permissionid);
			//System.out.println(list);
			return gyRolePermissionMapper.getpermissionIdToQuery(permissionid);
		}

	//左边：根据用户角色id(roleid)更新/插入角色权限(permissionId)
	public void editRoleToPermission(Integer roleid, Integer[] chks) throws Exception {
		//【3.1】先要进行查询gy_role_permission表中 是否已经存在该对应记录了如果不存则执行插入操作
		//，如果存在则不要任何操作
		List<GyRolePermission> rp_List=gyRolePermissionMapper.queryByRoleIdGetPermissionId(roleid);
		//[1]chks[1  3]-->permissionId[1  3]   不修改
		if(rp_List.size()!=chks.length) {
		//[2]chks[1 2 3]-->permissionId[1  3  4]
		 if(chks.length>rp_List.size()) {//1=chks.length>rp_List.size()=0
			 //进行添加插入操作
			 System.out.println("*******进行添加插入操作");
			 if(rp_List.size()==0) {
				 for(int i=0;i<chks.length;i++) {
					 GyRolePermission gyRolePermission=new GyRolePermission();
					 gyRolePermission.setRoleid(roleid);
					 gyRolePermission.setPermissionid(chks[i]);
					 gyRolePermissionMapper.insert(gyRolePermission);
				 }
				 return;
			 }
			 for(int i=0;i<chks.length&&rp_List.size()!=0;i++) {
				 int m=0;
				 while(m<rp_List.size()&&chks[i]!=rp_List.get(m).getPermissionid()) {
					 m++;
					 if(m==rp_List.size()) {//chks[i]即是新增的
						 GyRolePermission gyRolePermission=new GyRolePermission();
						 gyRolePermission.setRoleid(roleid);
						 gyRolePermission.setPermissionid(chks[i]);
						 gyRolePermissionMapper.insert(gyRolePermission);
					 }
				 }
			 }
			
			 
			 
		 }else {//进行删除操作
		//[3]chks[1  3]-->permissionId[1 2 3]  
			 System.out.println("*******执行删除操作");
			 if(chks.length==0) {
				 for(int i=0;i<rp_List.size();i++) {
					 GyRolePermission gy=new GyRolePermission();
		    		   gy.setRoleid(roleid);
		    		   gy.setPermissionid(rp_List.get(i).getPermissionid());
		    		   gyRolePermissionMapper.deleteByRoleIdAanPermissionId(gy);
				 }
				 return;
			 }
		for(int i=0;i<rp_List.size();i++) {
			  Integer permissionId=rp_List.get(i).getPermissionid();
		       for(int j=0;j<=chks.length;j++) {
		    	   if(j==chks.length) {
		    		 //说明新修改中没有之前的permissionid，即是执行删除操作（根据permissionid和roleid），跳出
		    		   GyRolePermission gy=new GyRolePermission();
		    		   gy.setRoleid(roleid);
		    		   gy.setPermissionid(permissionId);
		    		   gyRolePermissionMapper.deleteByRoleIdAanPermissionId(gy);
		    		   break;
		    	   }
		    	   //如果相等，则修改后仍然一样，不用操作
		    	   if(chks[j]==permissionId) {
		    		   break;
		    	     }
		    	   //执行插入或删除操作
		       }
		  }
		 }
		}
	}

	public void editPermissionToRole(Integer pid, Integer[] chks) throws Exception {
		
		List<GyRolePermission> rp_List=gyRolePermissionMapper.queryPermissionIdByGetRoleId(pid);
		if(rp_List.size()!=chks.length) {
		 if(chks.length>rp_List.size()) {
			 System.out.println("*******进行添加插入操作");
			 if(rp_List.size()==0) {
				 for(int i=0;i<chks.length;i++) {
					 GyRolePermission gyRolePermission=new GyRolePermission();
					 gyRolePermission.setRoleid(chks[i]);
					 gyRolePermission.setPermissionid(pid);
					 gyRolePermissionMapper.insert(gyRolePermission);
				 }
				 return;
			 }
			 for(int i=0;i<chks.length&&rp_List.size()!=0;i++) {
				 int m=0;
				 while(m<rp_List.size()&&chks[i]!=rp_List.get(m).getRoleid()) {
					 m++;
					 if(m==rp_List.size()) {
						 GyRolePermission gyRolePermission=new GyRolePermission();
						 gyRolePermission.setRoleid(chks[i]);
						 gyRolePermission.setPermissionid(pid);
						 gyRolePermissionMapper.insert(gyRolePermission);
					 }
				 }
			 }
			
			 
			 
		 }else {//进行删除操作
		//[3]chks[1  3]-->permissionId[1 2 3]  
			 System.out.println("*******执行删除操作");
			 if(chks.length==0) {
				 for(int i=0;i<rp_List.size();i++) {
					 GyRolePermission gy=new GyRolePermission();
		    		   gy.setRoleid(rp_List.get(i).getRoleid());
		    		   gy.setPermissionid(pid);
		    		   gyRolePermissionMapper.deleteByRoleIdAanPermissionId(gy);
				 }
				 return;
			 }
		for(int i=0;i<rp_List.size();i++) {
		       for(int j=0;j<=chks.length;j++) {
		    	   if(j==chks.length) {
		    		 //说明新修改中没有之前的roleId，即是执行删除操作（根据permissionid和roleId），跳出
		    		   GyRolePermission gy=new GyRolePermission();
		    		   gy.setRoleid(rp_List.get(i).getRoleid());
		    		   gy.setPermissionid(pid);
		    		   gyRolePermissionMapper.deleteByRoleIdAanPermissionId(gy);
		    		   break;
		    	   }
		    	   //如果相等，则修改后仍然一样，不用操作
		    	   if(chks[j]==rp_List.get(i).getRoleid()) {
		    		   break;
		    	     }
		    	   //执行插入或删除操作
		       }
		  }
		 }
		}
	
		
	}

	@Override
	public ListRoleVO listUser() {
		ListRoleVO list = new ListRoleVO();
		List<GyUser> userList= usermapper.selectAllEmploy();
		List<GyRole> rolelist=gyRolePermissionMapper.listRole();
		list.setRoleList(rolelist);
		list.setUserList(userList);
		return list;
	}

	@Override
	public String roleToUser(Integer uid, Integer[] roleids) {
		
		for(int i=0;i<roleids.length;i++) {
			int roleid=roleids[i];
			GyUserRole ur=new GyUserRole();
			ur.setUserid(uid);
			ur.setRoleid(roleid);
			int rs=userRoleMapper.selectByUserIdAndRoleId(ur);
			if(rs==0) {
				userRoleMapper.insertByUserIdAndRoleId(ur);
			}else {
				//先删除再插入
				userRoleMapper.deleteByUserIdAndRoleId(ur);
				userRoleMapper.insertByUserIdAndRoleId(ur);
			}
		}
		
		return "添加成功!";
	}
	
	
	
}
